Java 7 Recipes: A Problem-Solution Approach by Josh Juneau; Carl Dea; Freddy Guime; John O'Conner

Java 7 Recipes: A Problem-Solution Approach by Josh Juneau; Carl Dea; Freddy Guime; John O'Conner

Author:Josh Juneau; Carl Dea; Freddy Guime; John O'Conner
Language: eng
Format: epub
Publisher: Apress®
Published: 2011-12-31T05:00:00+00:00


KeyboardMoveBehavior keyboardMoveBehavior

= new KeyboardMoveBehavior(userMovementGroup, transformGroup);

keyboardMoveBehavior.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000f));

userTransformGroup.addChild(keyboardMoveBehavior);

transformGroup.addChild(userTransformGroup);

/// Class definition

class KeyboardMoveBehavior extends Behavior {

@Override

public void initialize() {

this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));

currentAxis = 0f;

}

@Override

public void processStimulus(Enumeration criteria) {

while (criteria.hasMoreElements()) {

Object element = criteria.nextElement();

if (element instanceof WakeupOnAWTEvent) {

WakeupOnAWTEvent event = (WakeupOnAWTEvent) element;

for (AWTEvent awtEvent : event.getAWTEvent()) {

if (awtEvent instanceof KeyEvent) {

KeyEvent keyEvent = (KeyEvent) awtEvent;

if (keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {

moveLeft();

} else if (keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {

moveRight();

} else if (keyEvent.getKeyCode() == KeyEvent.VK_UP) {

moveForward();

} else if (keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {

moveBackwards();

}

}

}

}

}

this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));

}

}

The KeyboardBehavior extends the Behavior class and implements the initialize() and processStimulus() methods. The initialize() method registers the stimuli that the behavior class will respond to. This recipe calls upon wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED)) to register an interest in any key that is pressed.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.